public class test {
}
class Solution {
    int[][] memo;
    public int minPathCost(int[][] grid, int[][] moveCost) {
        int ans = Integer.MAX_VALUE;
        int m = grid.length;
        int n = grid[0].length;
        memo = new int[m][n];
        for(int i=0; i<n; i++){
            ans = Math.min(dfs(grid,moveCost,0,i),ans);
        }
        return ans;
    }

    int dfs(int[][] grid, int[][] moveCost, int i, int j){
        if(i == grid.length-1)
            return grid[i][j];
        if(memo[i][j] != 0) return memo[i][j];
        int res = Integer.MAX_VALUE;
        for(int k=0; k<grid[0].length; k++){
            res = Math.min(res,dfs(grid,moveCost,i+1,k)+moveCost[grid[i][j]][k]);
        }
        return memo[i][j] = res + grid[i][j];
    }
}